home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / Macintosh Tracker 1.1 Source / Original Tracker 3.10 Source / sgi_audio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-22  |  3.3 KB  |  163 lines  |  [TEXT/KAHL]

  1. /* sgi_audio.c */
  2.  
  3. /* $Id: sgi_audio.c,v 3.4 1992/11/27 10:29:00 espie Exp espie $
  4.  * $Log: sgi_audio.c,v $
  5.  * Revision 3.4  1992/11/27  10:29:00  espie
  6.  * General cleanup
  7.  *
  8.  * Revision 3.3  1992/11/24  10:51:19  espie
  9.  * Added pseudo discardbuffer.
  10.  *
  11.  * Revision 3.2  1992/11/22  17:20:01  espie
  12.  * Checks for finetune ?
  13.  *
  14.  * Revision 3.1  1992/11/19  20:44:47  espie
  15.  * Protracker commands.
  16.  *
  17.  * Revision 3.0  1992/11/18  16:08:05  espie
  18.  * New release.
  19.  *
  20.  * Revision 2.11  1992/11/17  15:38:00  espie
  21.  * Dummy discard_buffer()
  22.  * Changed sync_audio value again.
  23.  * Added synchro for dump.
  24.  * Bug fix: must ask new frequency after we tried to set it to get it
  25.  * rounded up.
  26.  * Added stereo option (kind of).
  27.  * Separated mix/stereo stuff.
  28.  * Checked buffer size.
  29.  * Added possibility to get back to MONO for the sgi.
  30.  * Added stereo capabilities to the indigo version.
  31.  * Ask the frequency to the audio device.
  32.  * Corrected bug: when closing audio,
  33.  * we now wait for the samples queue to be empty.
  34.  */
  35.  
  36. #include <audio.h>
  37. #include <malloc.h>
  38. #include <stdio.h>
  39. #include "defs.h"
  40. #include "extern.h"
  41.  
  42. X int sginap(long ticks);
  43.      
  44. LOCAL char *id = "$Id: sgi_audio.c,v 3.4 1992/11/27 10:29:00 espie Exp espie $";
  45.  
  46. LOCAL signed short *buffer;
  47. LOCAL int index;
  48.  
  49. LOCAL int number;
  50. LOCAL BOOL sync = FALSE;
  51.  
  52. LOCAL ALport audio;
  53. LOCAL ALconfig config;
  54.  
  55. LOCAL BOOL donotwait = FALSE;
  56. LOCAL long chpars[] = {AL_OUTPUT_RATE, 0};
  57.  
  58. LOCAL int stereo;  /* are we playing stereo or not ? */
  59. /* 256th of primary/secondary source for that side. */
  60. LOCAL int primary, secondary;
  61.  
  62. void set_mix(percent)
  63. int percent;
  64.     {
  65.     percent *= 256;
  66.     percent /= 100;
  67.     primary = percent;
  68.     secondary = 512 - percent;
  69.     }
  70.  
  71. int open_audio(f, s)
  72. int f, s;
  73.     {
  74.  
  75.     donotwait = FALSE;
  76.     chpars[1] = f;
  77.     if (f != 0)
  78.         ALsetparams(AL_DEFAULT_DEVICE, chpars, 2);
  79.     ALgetparams(AL_DEFAULT_DEVICE, chpars, 2);
  80.     config = ALnewconfig();
  81.     stereo = s;
  82.     if (stereo)
  83.         {
  84.         ALsetchannels(config, AL_STEREO);
  85.         number = 2;
  86.         set_mix(30);
  87.         }
  88.     else
  89.         {
  90.         ALsetchannels(config, AL_MONO);
  91.         number = 1;
  92.         }
  93.     ALsetwidth(config, AL_SAMPLE_16);
  94.     audio = ALopenport("soundtracker mono", "w", config);
  95.     index = 0;
  96.     buffer = malloc(sizeof(signed short) * number * chpars[1]);
  97.     return chpars[1];
  98.     }
  99.  
  100. void set_synchro(s)
  101. BOOL s;
  102.     {
  103.     sync = s;
  104.     }
  105.  
  106. int update_frequency()
  107.     {
  108.     int oldfreq;
  109.  
  110.     oldfreq = chpars[1];
  111.     ALgetparams(AL_DEFAULT_DEVICE, chpars, 2);
  112.     if (chpars[1] != oldfreq)
  113.         {
  114.         buffer = realloc(buffer, sizeof(signed short) * number * chpars[1]);
  115.         return chpars[1];
  116.         }
  117.     else
  118.         return 0;
  119.     }
  120.  
  121.  
  122. void output_samples(int left, int right)
  123.     {
  124.     if (stereo)
  125.         {
  126.         buffer[index++] = (left * primary + right * secondary)/256;
  127.         buffer[index++] = (right * primary + left * secondary)/256;
  128.         }
  129.     else
  130.         buffer[index++] = left + right;
  131.     }
  132.  
  133. void flush_buffer(void)
  134.     {
  135.     ALwritesamps(audio, buffer, index);
  136.     if (sync)
  137.         while(ALgetfilled(audio) > index * 10)
  138.             /* busy wait */
  139.             ;
  140.     index = 0;
  141.     }
  142.  
  143. void discard_buffer(void)
  144.     {
  145.     donotwait = TRUE;
  146.     /* mostly not implemented, only working when using close_audio
  147.      * right after
  148.      */
  149.     }
  150.  
  151. void close_audio(void)
  152.     {
  153.     if (!donotwait)
  154.         {
  155.         while(ALgetfilled(audio) != 0)
  156.             sginap(1);
  157.         }
  158.     ALcloseport(audio);
  159.     ALfreeconfig(config);
  160.     free(buffer);
  161.     }
  162.  
  163.